package wangjg.security.interceptor.spring;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

import wangjg.security.SecurityContext;
import wangjg.security.aop.ExecutionRequest;
import wangjg.security.core.LoginModule;
import wangjg.security.core.validator.RequestValidator;

public class ExecutionAdvice implements MethodBeforeAdvice{
	private RequestValidator requestValidator;
	private LoginModule loginModule;
	
	public void before(Method method, Object[] args, Object target) throws Throwable {
		System.out.println("MethodInterceptor.");
		this.validate(method, args, target);
	}

	private void validate(final Method method,final Object[] args,final Object target){
		Object user = loginModule.login();
		ExecutionRequest request = new ExecutionRequest(method, args, target);
		boolean b = this.requestValidator.validate(request, user);
		
		if(!b){
			throw new RuntimeException("a x");
		}
	}
	
	private void init(){
		SecurityContext sc = SecurityContext.load("security-aop.xml");
		this.requestValidator = sc.getRequestValidator();
		this.loginModule = sc.getLoginModule();
	}
}
